using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using AjGenesisStudio.Syntax;
using AjGenesisStudio.Properties;

namespace AjGenesisStudio.Documents
{
    public partial class Document : Fireball.Docking.DockableWindow
    {
        public Document()
        {
            InitializeComponent();

            _filePath = string.Empty;
        }

        private string _filePath;
        public string FilePath
        {
            get { return _filePath; }
            set { 
                _filePath = value;
                SetTitle();
            }
        }

        private string def_extension;
        public string DefaultExtension
        {
            get { return def_extension; }
            set { def_extension = value; }
        }

        private void LoadContent()
        {
            string syntaxFile;

            if (File.Exists(_filePath))
            {
                editor.Open(_filePath);
                syntaxFile = SyntaxHelper.GetSyntaxFile(Path.GetExtension(_filePath));
            }
            else
                syntaxFile = SyntaxHelper.GetSyntaxFile(def_extension);

            if (!string.IsNullOrEmpty(syntaxFile))
                if (File.Exists(syntaxFile))
                    syntaxdoc.SyntaxFile = syntaxFile;
        }

        public void SetSyntax(string syn_path)
        {
            if (syntaxdoc.SyntaxFile != syn_path)
                syntaxdoc.SyntaxFile = syn_path;
        }

        public virtual void Open()
        {
            LoadContent();
        }

        private void editor_FileSavedChanged(object sender, EventArgs e)
        {
            SetTitle();
        }

        private void SetTitle()
        {
            string title;
            if (string.IsNullOrEmpty(_filePath))
            {
                title = "New*";
            }
            else
            {
                title = Path.GetFileName(_filePath);
                if (!editor.Saved)
                    title += "*";
            }

            this.Text = title;
            this.TabText = title;
        }

        private void editor_TextChanged(object sender, EventArgs e)
        {
            SetTitle();
        }

        private void Document_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult res;

            if (!editor.Saved)
            {
                res = MessageBox.Show("Do you want to save the changes you have done before close?", "Closing", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (res == DialogResult.Cancel)
                    e.Cancel = true;
                else if (res == DialogResult.Yes)
                    (new Commands.SaveCommand()).Execute(this);
            }
        }

        private void Document_Load(object sender, EventArgs e)
        {
            editor.FontSize = Settings.Default.FontSize;
        }

    }
}